package com.sky.controller.admin;

import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @author 猛在王
 * @version 1.0
 * @date 25/5/2024 下午1:39
 * @description DishController 菜品管理接口
 * @since 1.0
 */
@RestController
@Api(tags = "菜品接口")
@RequestMapping("/admin/dish")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品：{}",dishDTO);
        dishService.save(dishDTO);

        //通过分类id删除缓存
        String patter = "dish_"+dishDTO.getCategoryId();
        setDishRedis(patter);

        return Result.success();
    }

    /**
     * 实现菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询菜品")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除菜品")
    public Result delete(@RequestParam List<Long> ids){
        log.info("批量删除菜品：{}",ids);
        dishService.delete(ids);

        //清理所有缓存
        setDishRedis("dish_*");
        return Result.success();
    }

    /**
     * 查询菜品详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("查询菜品详情")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("查询菜品详情:{}",id);
        DishVO dishVo = dishService.getByIdWithFlavor(id);
        return Result.success(dishVo);
    }

    /**
     * 修改菜品详情
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品详情")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品详情:{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

        //清理所有缓存
        setDishRedis("dish_*");
        return Result.success();

    }

    /**
     * 搜索菜品
     * @param name
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("通过菜品名字搜索菜品")
    public Result<List<Dish>> list(String name,Long categoryId){
        log.info("通过菜品名字搜索菜品，参数为：{}",name,categoryId);
        List<Dish> dishes = dishService.listByName(name,categoryId);
        return Result.success(dishes);
    }

    /**
     * 启用停用菜品
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用停用菜品")
    public Result startOrStop(@PathVariable Integer status, Long id){
        log.info("启用停用菜品");
        dishService.startOrStop(status,id);

        //清理所有缓存
        setDishRedis("dish_*");
        return Result.success();
    }

    /**
     * 删除缓存
     * @param patter
     */
    private void setDishRedis(String patter){
        Set keys = redisTemplate.keys(patter);
        redisTemplate.delete(keys);
    }


}















